import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  // 提供唯一公共数据源
  state: {
    user: {
      id: '',
      qq: '',
      email: '',
      nickName: '',
      phone: '',
      headPic: '',
      name: '',
      isMobileCheck: false,
      isEmailCheck: false,
      sex: 0,
      userLevel: 1,
      points: 0,
      experienceValue: 0,
      birthday: '',
      authorities: []
    }
  },
  // 只有mutation中定义的函数才有修改state里数据的权力
  // 不要再mutations中执行异步操作，如果要执行异步操作到action间接出发mutations中方法，不然显示数据变了store中数据不变
  mutations: {
    updateUserId (state, id) {
      state.user.id = id
    },
    updateUserHeadPic (state, headPic) {
      state.user.headPic = headPic
    },
    updateUserPhone (state, phone) {
      state.user.phone = phone
    },
    updateUserNickName (state, nickName) {
      state.user.nickName = nickName
    },
    updateUserEmail (state, email) {
      state.user.email = email
    },
    updateUserQQ (state, qq) {
      state.user.qq = qq
    },
    updateUserName (state, name) {
      state.user.name = name
    },
    updateUserIsMobileCheck (state, isMobileCheck) {
      state.user.isMobileCheck = isMobileCheck
    },
    updateUserIsEmailCheck (state, isEmailCheck) {
      state.user.isEmailCheck = isEmailCheck
    },
    updateUserSex (state, sex) {
      state.user.sex = sex
    },
    updateUseruSerLevel (state, userLevel) {
      state.user.userLevel = userLevel
    },
    updateUserPoints (state, points) {
      state.user.points = points
    },
    updateUserExperienceValue (state, experienceValue) {
      state.user.experienceValue = experienceValue
    },
    updateUserBirthday (state, birthday) {
      state.user.birthday = birthday
    },
    updateUserAuthorities (state, authorities) {
      state.user.authorities = authorities.split(state, ',')
    },
    updateUserAll (state, user) {
      state.user.id = user.id
      state.user.qq = user.qq
      state.user.email = user.email
      state.user.nickName = user.nickName
      state.user.phone = user.phone
      state.user.headPic = user.headPic
      state.user.name = user.name
      state.user.isMobileCheck = user.isMobileCheck
      state.user.isEmailCheck = user.isEmailCheck
      state.user.sex = user.sex
      state.user.userLevel = user.userLevel
      state.user.points = user.points
      state.user.experienceValue = user.experienceValue
      state.user.birthday = user.birthday
      state.user.authorities = user.authorities
      state.user.accessToken = user.accessToken
    },
    cleanUser (state) {
      state.user.id = ''
      state.user.qq = ''
      state.user.email = ''
      state.user.nickName = ''
      state.user.phone = ''
      state.user.headPic = ''
      state.user.name = ''
      state.user.isMobileCheck = false
      state.user.isEmailCheck = false
      state.user.sex = 0
      state.user.userLevel = 1
      state.user.points = 0
      state.user.experienceValue = 0
      state.user.birthday = ''
      state.user.authorities = []
    }
  },
  // 在actions中,不能直接修改state 中的数据;
  // 必须通过context. commit(state, )触发某个mutation才行
  actions: {
  },
  modules: {
  },
  // Getter可以对Store中已有的数据加I处理之后形成新的数据，类似Vue的计算属性。
  // Store中数据发生变化，Getter 的数据也会跟着变化。
  getters: {
    getUserId (state) {
      return state.user.id
    },
    getUserHeadPic (state) {
      return state.user.headPic
    },
    getUserPhone (state) {
      return state.user.phone
    },
    getUserNickName (state) {
      return state.user.nickName
    },
    getUserEmail (state) {
      return state.user.email
    },
    getUserQQ (state) {
      return state.user.qq
    },
    getUserName (state) {
      return state.user.name
    },
    getUserIsMobileCheck (state) {
      return state.user.isMobileCheck
    },
    getUserIsEmailCheck (state) {
      return state.user.isEmailCheck
    },
    getUserSex (state) {
      return state.user.sex
    },
    getUserUserLevel (state) {
      return state.user.userLevel
    },
    getUserPoints (state) {
      return state.user.points
    },
    getUserExperienceValue (state) {
      return state.user.experienceValue
    },
    getUserBirthday (state) {
      return state.user.birthday
    },
    getUserAuthorities (state) {
      return state.user.authorities
    },
    getUser (state) {
      return state.user
    }
  }
})
